<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "supplier_category".
 *
 * @property int $id
 * @property string $name
 * @property int $status
 * @property int $sort
 * @property string $create_time
 * @property string $update_time
 */
class SupplierCategory extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'supplier_category';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['status', 'sort'], 'integer'],
            [['create_time', 'update_time'], 'safe'],
            [['name'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'status' => 'Status',
            'sort' => 'Sort',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $time = date('Y-m-d H:i:s');
            $this->update_time = $time;
            if ($insert) {
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }

    // 只获取最近的一张
    public function getImage()
    {
        return $this->hasOne(SupplierImageCategory::class, ['supplier_category_id' => 'id'])
            ->orderBy(['id' => SORT_DESC]);
    }
}
